using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Model.Core.DbContexts;
using Model.Core.Entity;
using Model.Core.Entity.Jwts;
using RobotWebApiService.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace RobotWebApiService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class RoleController : ControllerBase
    {
        private readonly MyDbContext _context;

        public RoleController(MyDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        [HttpGet]
        [SwaggerResponse(200, Type = typeof(ApiResponse<IEnumerable<Role>>))]
        public async Task<ApiResponse<IEnumerable<Role>>> GetRoles(int pageNumber = 1, int pageSize = 10)
        {
            try
            {
                var query = _context.Role
                    .Where(r => !r.IsDeleted)
                    .OrderBy(r => r.CreatedTime);

                var totalCount = await query.CountAsync();
                var items = await query
                    .Skip((pageNumber - 1) * pageSize)
                    .Take(pageSize)
                    .ToListAsync();

                return new PagedApiResponse<IEnumerable<Role>>(true, "", items, pageNumber, pageSize, totalCount);
            }
            catch (Exception ex)
            {
                return new ApiResponse<IEnumerable<Role>>(false, ex.Message);
            }
        }

        /// <summary>
        /// 获取单个角色详情
        /// </summary>
        [HttpGet("{id}")]
        [SwaggerResponse(200, Type = typeof(ApiResponse<Role>))]
        [SwaggerResponse(404, Type = typeof(ApiResponse<string>))]
        public async Task<ApiResponse<Role>> GetRole(Guid id)
        {
            try
            {
                var role = await _context.Role
                    .FirstOrDefaultAsync(r => r.Id == id && !r.IsDeleted);

                return role == null
                    ? new ApiResponse<Role>(false, "角色不存在")
                    : new ApiResponse<Role>(true, "", role);
            }
            catch (Exception ex)
            {
                return new ApiResponse<Role>(false, ex.Message);
            }
        }

        /// <summary>
        /// 创建新角色
        /// </summary>
        [HttpPost]
        [SwaggerResponse(201, Type = typeof(ApiResponse<Role>))]
        public async Task<ApiResponse<Role>> CreateRole([FromBody] Role role)
        {
            try
            {
                role.Id = role.Id == Guid.Empty ? Guid.NewGuid() : role.Id;
                _context.Role.Add(role);
                await _context.SaveChangesAsync();
                return new ApiResponse<Role>(true, "创建成功", role);
            }
            catch (Exception ex)
            {
                return new ApiResponse<Role>(false, ex.Message);
            }
        }

        /// <summary>
        /// 更新角色信息
        /// </summary>
        [HttpPut("{id}")]
        [SwaggerResponse(200, Type = typeof(ApiResponse<Role>))]
        [SwaggerResponse(400, Type = typeof(ApiResponse<string>))]
        public async Task<ApiResponse<Role>> UpdateRole(Guid id, [FromBody] Role role)
        {
            try
            {
                if (id != role.Id) return new ApiResponse<Role>(false, "ID不匹配");

                _context.Entry(role).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return new ApiResponse<Role>(true, "更新成功", role);
            }
            catch (Exception ex)
            {
                return new ApiResponse<Role>(false, ex.Message);
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpDelete("{id}")]
        [SwaggerResponse(200, Type = typeof(ApiResponse<string>))]
        [SwaggerResponse(404, Type = typeof(ApiResponse<string>))]
        public async Task<ApiResponse<string>> DeleteRole(Guid id)
        {
            try
            {
                var role = await _context.Role.FindAsync(id);
                if (role == null) return new ApiResponse<string>(false, "角色不存在");

                role.IsDeleted = true;
                await _context.SaveChangesAsync();
                return new ApiResponse<string>(true, "删除成功");
            }
            catch (Exception ex)
            {
                return new ApiResponse<string>(false, ex.Message);
            }
        }
    }
}